import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getCartListService, addCartService, deleteCartService } from '@/api/shop'
export const useShopStore = defineStore(
  'shop',
  () => {
    const shopList = ref([])

    const getShopList = async (id) => {
      const result = await getCartListService(id)
      shopList.value = result.data
    }

    const addToCart = async (obj) => {
      await addCartService(obj)
    }

    const removeFromCart = async (id) => {
      await deleteCartService(id)
    }
    //获取该用户的购物车数量
    const getCartCount = async (id) => {
      await getShopList(id)
      return shopList.value.length
    }

    return {
      shopList,
      getShopList,
      addToCart,
      removeFromCart,
      getCartCount
    }
  },
  {
    persist: true
  }
)
